package com.huika.huikalib.utils;

import java.math.RoundingMode;
import java.text.DecimalFormat;
/**
 * 
 * @author fanxing 创建于 Dec 17, 2014
 */
public class MoneyShowTool {
	private static DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");//格式化设置
	private static DecimalFormat twolastDF = new DecimalFormat("#0.00");
	private static DecimalFormat twoDF = new DecimalFormat("#0.00");
	private static DecimalFormat oneDF = new DecimalFormat("#0.0");
	
	private static DecimalFormat nolastDF = new DecimalFormat("#0");
	private static DecimalFormat formatnolastDF = new DecimalFormat("#,##0");
	
	/**单独后面两位小数点*/

	public static String format(double d){
		return decimalFormat.format(d);
	}
	
	public static String formatTwo(double d){
		return twoDF.format(d);
	}
	public static String formatOne(double d){
		return oneDF.format(d);
	}
	public static String formatOneStr(String d){
		if (StringUtils.isEmpty(d)) {
			 return "0.0";
		}
		double dd = 0;
		try {
			dd = Double.parseDouble(d);
		} catch (Exception e) {
			return d;
		}
		return oneDF.format(dd);
	}
	
	/**
	 * 
	 * @description：是否需要进行四舍五入  flase 需要 true 不需要
	 * @author zc
	 * @date 2015年7月10日 下午2:26:03
	 */
	public static String twolastDF(double d, boolean isDown) {
		if(isDown) {
			twolastDF.setRoundingMode(RoundingMode.DOWN);
		} else {
			twolastDF.setRoundingMode(RoundingMode.HALF_UP);
		}
		return twolastDF.format(d);
	}
	
	public static String twolastDF(double d){
		return twolastDF(d, true);
	}
	public static String twolastDFStr(String d){
		double dd = 0;
		try {
			 dd = Double.parseDouble(d);
		} catch (Exception e) {
			return d;
		}
		twolastDF.setRoundingMode(RoundingMode.DOWN);//取消四舍五入，计算投资收益用到  modify by zc 2015/06/09
		return twolastDF.format(dd);
	}
	
	public static String nolastDF(double d){
		return nolastDF(d, false);
	}

	public static String nolastDF(double d, boolean isDown){
		if(isDown){
			nolastDF.setRoundingMode(RoundingMode.DOWN);
		}else{
			nolastDF.setRoundingMode(RoundingMode.HALF_UP);
		}
		return nolastDF.format(d);
	}

	public static String nolastDFStr(String d){
		double dd = 0;
		try {
			dd = Double.parseDouble(d);
		} catch (Exception e) {
			return d;
		}
		nolastDF.setRoundingMode(RoundingMode.UP);
		return nolastDF.format(dd);
	}


	public static String formatNolastDF(double d){
		return formatnolastDF.format(d);
	}
}
